import 'package:catsao/mock/video.dart';
import 'package:catsao/pages/TictokhomePage.dart';
import 'package:catsao/pages/cameraPage.dart';
import 'package:catsao/pages/msgPage.dart';
import 'package:catsao/pages/userPage.dart';
import 'package:catsao/style/theme.dart';
import 'package:catsao/util/event_bus.dart';
import 'package:catsao/views/tiktokTabBar.dart';

import 'package:flutter/material.dart';

import 'package:catsao/util/stirngs_helper.dart';
import 'package:fluwx/fluwx.dart';

import 'home_page_toptabbar.dart';

/**
 * home page of application.
 */
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: StringResources.APP_NAME,
      color: Colors.white,
      // theme: ThemeColor.themecolor,
      home: new HomePage(title: StringResources.APP_NAME),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  TikTokPageTag tabBarType = TikTokPageTag.home;
  List<UserVideo> videoDataList = [];

  List<Widget> pages = new List();

  @override
  void initState() {
    pages
      ..add(HomePageTopTabBar())
      ..add(TictokHomePage())
      ..add(
        MaterialApp(
            theme: ThemeColor.themecolor,
            home: Scaffold(
              body: MsgPage(),
            )),
      )
      ..add(

        UserPage(isSelfPage: true),
      );
    super.initState();

    eventBusListen();
    videoDataList = UserVideo.fetchVideo();

    _initFluwx();
  }

  void eventBusListen() {
    eventBus.on<EventBusVideoControll>().listen((event) {
      eventBus.fire(new EventBusVideoPlay("play"));
      eventBus.fire(new EventBusVideoId(event.videoid));
      setState(() {
        tabBarType = TikTokPageTag.video;
      });
    });
  }

  @override
  void dispose() {
    eventBus.destroy();
    super.dispose();
  }

  //
  _initFluwx() async {
    await registerWxApi(
        appId: "wxe3730784d966e8a1",
        doOnAndroid: true,
        doOnIOS: true,
        universalLink: "https://your.univerallink.com/link/");
  }

  @override
  Widget build(BuildContext context) {
    int currentPage = 0;
    switch (tabBarType) {
      default:
        currentPage = tabBarType.index;
    }
    double a = MediaQuery.of(context).size.aspectRatio;
    bool hasBottomPadding = a < 0.55;
    bool hasBackground = hasBottomPadding;
    hasBackground = tabBarType != TikTokPageTag.home;
    if (hasBottomPadding) {
      hasBackground = true;
    }

    Widget tikTokTabBar = Theme(
      data: ThemeColor.themecolor,
      child: TikTokTabBar(
        hasBackground: hasBackground,
        current: tabBarType,
        onTabSwitch: (type) async {
          setState(() {
            tabBarType = type;
            if (type == TikTokPageTag.video) {
              eventBus.fire(new EventBusVideoPlay("play"));
            } else {
              eventBus.fire(new EventBusVideoPlay("pause"));
            }
          });
        },
        onAddButton: () {
          Navigator.of(context).push(
            MaterialPageRoute(
                fullscreenDialog: true,
                builder: (context) {
                  return Theme(
                    data: ThemeColor.themecolor,
                    child: CameraPage(),
                  );
                }),
          );
        },
      ),
    );
    return new Scaffold(
        bottomNavigationBar: tikTokTabBar,
        body:
            // pages[currentPage]
            Stack(
          children: [
            Offstage(
              offstage: currentPage != 0,
              child: pages[0],
            ),
            Offstage(
              offstage: currentPage != 1,
              child: pages[1],
            ),
            Offstage(
              offstage: currentPage != 2,
              child: pages[2],
            ),
            Offstage(
              offstage: currentPage != 3,
              child: pages[3],
            ),
          ],
        ));
  }
}
